# -*- coding: utf-8 -*-
# created on 2016/10/19


from mathsolver.functions.base import *


class IsEqual(BaseFunction):
    def solver(self, *args):
        if isinstance(args[0], BaseSymbolValue) and isinstance(args[1], BaseSymbolValue):
            poly1 = args[0].sympify()
            poly2 = args[1].sympify()
            new_poly1 = poly1[poly1.keys()[0]].args[0]
            new_poly2 = poly2[poly2.keys()[0]].args[0]
        elif isinstance(args[0], BaseSymbolValue) and isinstance(args[1], BaseValue):
            poly1 = args[0].sympify()
            poly2 = args[1].sympify()
            new_poly1 = poly1[poly1.keys()[0]].args[0]
            new_poly2 = poly2
        elif isinstance(args[0], BaseValue) and isinstance(args[1], BaseSymbolValue):
            poly1 = args[0].sympify()
            poly2 = args[1].sympify()
            new_poly1 = poly1
            new_poly2 = poly2[poly2.keys()[0]].args[0]
        elif isinstance(args[0], BaseValue) and isinstance(args[1], BaseValue):
            new_poly1 = args[0].sympify()
            new_poly2 = args[1].sympify()
        eq = [new_poly1, new_poly2]
        self.steps.append(["由题意,得", self.output_eq(eq)])
        self.output.append(BaseEq(eq))
        self.label.add("值相等")
        return self


# 代数式值大于某个代数式
class IsDaYu(BaseFunction):
    op = ">"
    label_answer = "值大于"

    def solver(self, *args):
        if isinstance(args[0], BaseSymbolValue):
            jies = args[0].sympify()
            poly1 = jies[jies.keys()[0]].args[0]
        else:
            poly1 = args[0].sympify()
        poly2 = args[1].sympify()
        if self.op == "=":
            eq = [poly1, poly2]
            self.steps.append(["由题意,得", self.output_eq(eq)])
            self.output.append(BaseEq(eq))
            self.label.add(self.label_answer)
        else:
            ineq = [poly1, self.op, poly2]
            self.steps.append(["由题意,得", self.output_eq(ineq)])
            self.output.append(BaseIneq(ineq))
            self.label.add(self.label_answer)
        return self


# 代数式值小于某个代数式
class IsXiaoYu(IsDaYu):
    op = "<"
    label_answer = "值小于"


# 代数式值不大于某个代数式
class IsXiaoEqual(IsDaYu):
    op = "<="
    label_answer = "值不大于"


# 代数式值不小于某个代数式
class IsDaEqual(IsDaYu):
    op = ">="
    label_answer = "值不小于"


# 代数式的和
class IsSum(BaseFunction):
    def solver(self, *args):
        value1 = args[0].sympify()
        value2 = args[1].sympify()
        value_sum = args[2].sympify()
        self.output.append(BaseEq([value1 + value2, value_sum]))
        return self


class JieYuZhiJian(BaseFunction):
    def solver(self, *args):
        value_left = args[0].sympify()
        if isinstance(args[1], BaseSymbolValue):
            jie = args[1].sympify()
            value_middle = jie[jie.keys()[0]].args[0]
        value_right = args[2].sympify()
        self.steps.append(["∴", BaseIneqs([value_left, "<", value_middle, "<", value_right]).printing()])
        self.output.append(BaseIneqs([value_left, "<", value_middle, "<", value_right]))
        return self


# 解的和
class JieSum(BaseFunction):
    def solver(self, *args):
        jies = args[0].sympify()
        jies_value = jies[jies.keys()[0]]
        answer_sum = 0
        for item in jies_value:
            answer_sum += item
        self.steps.append(["", "∴解的和为%s" % (new_latex(answer_sum))])
        self.output.append(BaseValue(answer_sum))
        return self


if __name__ == '__main__':
    pass
